package com.weixx.yctp.user.controller;

import cc.eamon.open.status.Status;
import cc.eamon.open.status.StatusCode;
import com.weixx.yctp.user.entity.User;
import com.weixx.yctp.user.entity.UserPostMapper;
import com.weixx.yctp.user.entity.UserUpdateMapper;
import com.weixx.yctp.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Angel on 2019/7/23.
 */

@Api(
        value = "用户-用户注册",
        tags = "用户-用户注册"
)
@RestController
@RequestMapping("/register")
public class RegisterController {

    @Autowired
    private UserService userService;



    @ApiOperation(
            value = "用户注册",
            notes = "用户注册"
    )
    @Transactional(
            rollbackFor = Exception.class
    )
    @RequestMapping(
            value = "",
            method = RequestMethod.POST
    )
    @ResponseBody
    public Status postRegisterMap(@RequestBody UserPostMapper entityPostMapper, HttpServletRequest request) throws Exception {
        String userName = null;
        if(entityPostMapper!=null){
            User entity = entityPostMapper.getEntity();
            userName = entity.getName();
        } else {
            userName = (String) request.getAttribute("userName");
        }
        return new Status(
                true,
                StatusCode.getCode("SUCCESS"),
                userService.postRegisterUser(entityPostMapper, userName, userName),
                "注册成功");
    }

    @ApiOperation(
            value = "UUID更新",
            notes = "UUID更新"
    )
    @Transactional(
            rollbackFor = Exception.class
    )
    @RequestMapping(
            value = "/uuid",
            method = RequestMethod.POST
    )
    @ResponseBody
    public Status postRegisterMapUpdateUUID(@RequestParam Integer id,
                                            @RequestParam String uuid) throws Exception {
        return new Status(
                true,
                StatusCode.getCode("SUCCESS"),
                userService.UpdateUserUUIDById(id, uuid),
                "uuid更新成功");
    }



    @ApiOperation(
            value = "更新用户信息 PASS 测试内容：不存在更新",
            notes = "更新用户信息 PASS 测试内容：不存在更新"
    )
    @Transactional(
            rollbackFor = Exception.class
    )
    @RequestMapping(
            value = "update",
            method = RequestMethod.POST
    )
    @ResponseBody
    public Status updateUserMap(@RequestBody UserUpdateMapper entityUpdateMapper, HttpServletRequest request) throws Exception {
        String userName = (String) request.getAttribute("userName");
        return new Status(
                true,
                StatusCode.getCode("SUCCESS"),
                userService.updateUser(entityUpdateMapper, userName),
                "更新成功");
    }

    @ApiOperation(
            value = "发送验证码",
            notes = "发送验证码"
    )
    @Transactional(
            rollbackFor = Exception.class
    )
    @RequestMapping(
            value = "sendSMS",
            method = RequestMethod.POST
    )
    @ResponseBody
    public Status sendSMS(@RequestParam String mobile,
                          @RequestParam String code) throws Exception {
        return new Status(
                true,
                StatusCode.getCode("SUCCESS"),
                userService.sendSMS(mobile, code),
                "发送成功");
    }

//    @ApiOperation(
//            value = "更新用户状态0-未激活 1-已激活",
//            notes = "更新用户状态0-未激活 1-已激活"
//    )
//    @Transactional(
//            rollbackFor = Exception.class
//    )
//    @RequestMapping(
//            value = "{uuid}/{status}",
//            method = RequestMethod.PUT
//    )
//    @ResponseBody
//    @PermissionLimit
//    public Status updateAppAlumniMap(@PathVariable String uuid,
//                                     @PathVariable Boolean status, HttpServletRequest request) throws
//            Exception {
//        String userName = (String) request.getAttribute("userName");
//        return new Status(
//                true,
//                StatusCode.getCode("SUCCESS"),
//                appAlumniService.updateUserStatus(uuid, status, userName),
//                "激活成功");
//    }


}
